An array consisting of n
integers is given. Find the sum and the count of the array elements whose
values are greater than the arithmetic mean of all elements.
Input. The first line contains an integer n (1 ≤ n ≤ 100) – the number of elements in the array. The second line contains n
integers, each does not exceed 100 in absolute value.
Output. Print the sum and the count of the array
elements whose values are greater than the arithmetic mean of all elements.
Sample input |
Sample output |
5 1 6 2 6 3 |
12 2 |
arrays
First, compute the sum s of the given numbers. The arithmetic
mean of all the numbers is s / n. Next, find the sum and the count of
numbers greater than the arithmetic mean.
Example
In the
example, n = 5 numbers
are given. Their sum is s = 1 + 6 + 2 + 6 + 3 = 18. The arithmetic mean of the numbers is s / n = 18 / 5 = 3.6. There
are two numbers greater than 3.6: these are the two sixes. Their sum is 12.
Algorithm
implementation
Read the input array. Compute the sum of its elements.
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d",&m[i]);
s += m[i];
}
Compute the sum sum and the count cnt of the elements m[i] in the
array that are greater than the arithmetic mean s / n. Note
that the inequality m[i] > s / n is equivalent to m[i] * n > s.
sum = cnt = 0;
for(i = 0; i < n; i++)
if (m[i] * n > s) //
m[i] > s / n
{
sum += m[i];
cnt++;
}
Print the answer.
printf("%d %d\n",sum,cnt);
Python implementation
Read the
input data.
n = int(input())
lst = list(map(int, input().split()))
Compute the arithmetic mean of the list.
ave = sum(lst) / len(lst)
Create a list of numbers greater
than the arithmetic mean.
above_ave = [x for x in
lst if x > ave]
Compute the sum and the count of
the numbers greater than the arithmetic mean.
sum = sum(above_ave)
cnt = len(above_ave)
Print the answer.
print(sum, cnt)